package xyz.naoyu.servlets.service.impl;

import com.alibaba.fastjson.JSON;
import xyz.naoyu.dao.BaseDao;

import xyz.naoyu.entity.Order;
import xyz.naoyu.entity.Talk;
import xyz.naoyu.servlets.service.CurdService;
import xyz.naoyu.settings;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yWieLoc
 * @date 2022/3/18 23:42
 */

@WebServlet("/api/order")
public class OrderServlet extends BaseServlet implements CurdService {
    @Override
    public void edit(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();
        Order order = new Order(req.getParameter("Id"));


        try {
            // 后端信息
            order = (Order) order.getInfo(tools);

            // 新建
            if(order.getCreatorId() == null ) {
                order = (Order) JSON.parse(req.getParameter("order"));

                // 设定系统信息
                order.setCreatorId((String) req.getSession().getAttribute("Id"));
                order.setCreateTime(String.valueOf(System.currentTimeMillis()));

                // 更新
            } else {
                

                // 用户编辑，认证
                if(order.getCreatorId() != req.getSession().getAttribute("Id") ) {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(false,"无权限",null)
                    ));
                    return;
                }
                // 加载参数
                order = JSON.parseObject(req.getParameter("order"),order.getClass());
                order.setType("Order");
            }

            // 传向后端
            if(order.infoUpdate(tools) == 0) {
                String cc = JSON.toJSONString(
                        new BackCode(false,"更新失败",null)
                );
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false,"更新失败",null)
                ));
                return;
            } else {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(true,"更新成功",null)
                ));
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void show(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();      // 连接工具
        Order order = new Order(req.getParameter("Id"));

        try {
            // 后端数据
            order = (Order) order.getInfo(tools);

            if(order.getCreatorId() == null) {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false,"未找到数据",null)
                ));
                return;
            }

            //  锁定
            if(order.getState() == null || order.getState().equals(settings.state.getString("LOCK")))  {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false,"已锁定",null)
                ));
                return;
            } else {

                // 没锁
                // TODO: 2022/3/19 观察权限 
                order.InfoLimit();
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(true,null, JSON.toJSONString(order))
                ));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void del(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();
        Order order = new Order("Id");


        try {
            order = (Order) order.getInfo(tools);

            if(order.getCreatorId()!=null && order.getCreatorId() == req.getSession().getAttribute("Id")) {
                if(order.delSelf(tools)) {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(true, null,null)
                    ));
                }else {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(false, "删除失败",null)
                    ));
                }
            } else {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false, "权限不足",null)
                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
